package com.hermit.qmst.tools;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.math.RoundingMode;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by hermithermit on 15/2/13.
 */
public class Utils {
    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
    private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符

    public static String replaceBlank(String paramString) {
        String str = "";
        if (paramString != null)
            str = Pattern.compile("\\s*|\t|\r|\n").matcher(paramString).replaceAll("");
        return str;
    }

    public static String analysePhoneNumber(String paramString) {
        String str1 = replaceBlank(paramString);
        str1 = str1.replace(" ", "").replace("-", "");
        int i = str1.indexOf("+86");
        if (i != -1)
            str1 = str1.substring(i + 3);
        int j = str1.indexOf("+");
        if (j != -1)
            str1 = str1.substring(j + 1);
        if (str1.length() > 5) {
            String str2 = str1.substring(0, 5);
            if ((str2.equals("17951")) || (str2.equals("12593")) || (str2.equals("17911")) || (str2.equals("10193")) || (str2.equals("17909")))
                str1 = str1.substring(5);
        }
        return str1;
    }

    public static String getPinYin(String str) {
        if (str == null) {
            return "#";
        }
        ArrayList<HanziToPinyin.Token> tokens = HanziToPinyin.getInstance().get(str);
        StringBuilder sb = new StringBuilder();
        if (tokens != null && tokens.size() > 0) {
            for (HanziToPinyin.Token token : tokens) {
                if (HanziToPinyin.Token.PINYIN == token.type) {
                    sb.append(token.target);
                    sb.append(" ");
                } else {
                    sb.append(token.source);
                    sb.append(" ");
                }
            }
        }
        return sb.toString().toLowerCase();
    }


    public static String getFirstLetter(String pinYin) {
        if (pinYin == null || pinYin.trim().length() == 0) {
            return "#";
        } else {
            String s;
            char pingyin = pinYin.toCharArray()[0];
            try {
                String[] sarry = PinyinHelper.toHanyuPinyinStringArray(pingyin);
                s = sarry[0].toUpperCase();

            } catch (Exception e) {
                e.printStackTrace();
                s = pingyin + "";
            }
            android.util.Log.i("getFirstLetter", "getFirstLetter:汉字==" + pinYin + "==s:" + s);
            return s.substring(0, 1);
        }

    }


    //判断是否是手机号码
    public static boolean isPhone(String phone) {

        return Pattern.compile("^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,5-9]))\\d{8,9}$").matcher(phone).matches();
    }

    public static String getMD5(String pStrMD5) {

        char[] strToChar;
        byte[] charToByte;
        byte[] md5Byte;
        StringBuilder builder;
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            strToChar = pStrMD5.toCharArray();
            charToByte = new byte[strToChar.length];
            for (int i = 0; i < strToChar.length; i++) {
                charToByte[i] = (byte) strToChar[i];
            }
            builder = new StringBuilder();
            md5Byte = digest.digest(charToByte);
            for (int j = 0; j < md5Byte.length; j++) {
                int k = 0xFF & md5Byte[j];
                if (k < 16) {
                    builder.append("0");
                }
                builder.append(Integer.toHexString(k));
            }
            return builder.toString();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取两个时间相差的天数
     *
     * @param beginTime 开始的时间
     * @param endTime   结束的时间
     * @return 时间天数
     */
    public static int getDateDay(String beginTime, String endTime) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// 输入日期的格式
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(beginTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date date2 = null;
        try {
            date2 = simpleDateFormat.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        GregorianCalendar cal1 = new GregorianCalendar();
        GregorianCalendar cal2 = new GregorianCalendar();
        cal1.setTime(date1);
        cal2.setTime(date2);
        int dayCount = (int) ((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / (1000 * 3600 * 24));// 从间隔毫秒变成间隔天数
        return dayCount;
    }

    public static String delHTMLTag(String htmlStr) {
//        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
//        Matcher m_script = p_script.matcher(htmlStr);
//        htmlStr = m_script.replaceAll(""); // 过滤script标签
//
//        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
//        Matcher m_style = p_style.matcher(htmlStr);
//        htmlStr = m_style.replaceAll(""); // 过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签

//        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
//        Matcher m_space = p_space.matcher(htmlStr);
//        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
        return htmlStr.trim(); // 返回文本字符串
    }

    public static String getTextFromHtml(String htmlStr) {
        com.hermit.qmst.tools.Log.i("delHTMLTag1", htmlStr, true);
        htmlStr = delHTMLTag(htmlStr);
        com.hermit.qmst.tools.Log.i("delHTMLTag2", delHTMLTag(htmlStr), true);
        htmlStr = htmlStr.replaceAll("&nbsp;", "");
        htmlStr = htmlStr.substring(0, htmlStr.indexOf("。") + 1);
        com.hermit.qmst.tools.Log.i("delHTMLTag3", htmlStr, true);
        return htmlStr;
    }

    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        System.out.println(m.matches() + "---");
        return m.matches();
    }

    public static String stringToInt(String s) {
        try {
            DecimalFormat df = new DecimalFormat("0.00");
            df.setRoundingMode(RoundingMode.HALF_UP);
            return df.format(Double.parseDouble(s));
        } catch (Exception e) {
            return "0.00";
        }
    }


    /*
* 将时间戳转换为时间
*/
    public static String stampToDate(String s){
        String str=s+"000";
        android.util.Log.i("stampToDate",str);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(str)));
        return sd;
//        String res;
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        long lt = new Long(s);
//        Date date = new Date(lt);
//        res = simpleDateFormat.format(date);
//        return res;
    }
}
